package com.gz.moreaseManager.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.gz.moreaseManager.sys.entity.Role;
import com.gz.moreaseManager.sys.entity.RoleMenu;
import com.gz.moreaseManager.sys.mapper.RoleMapper;
import com.gz.moreaseManager.sys.mapper.RoleMenuMapper;
import com.gz.moreaseManager.sys.service.IRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author wike
 * @since 2019-02-19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public boolean updateRoleStatusByIds(Byte status, Integer... roleIds) {

        int rs = roleMapper.updateRoleStatusByIds(status,Arrays.asList(roleIds));
        if( rs == roleIds.length )
            return true;
        return false;
    }

    @Override
    public boolean checkRoleInfoHasExist(Integer roleId, String roleName) {

        if( UtilTool.isBlank( roleId ) && UtilTool.isBlank( roleName))
            return false;
        //修改的时候查询
        if( UtilTool.isNotBlank( roleId ) && UtilTool.isNotBlank( roleName )){
            List<Role> list = roleMapper.selectList(new EntityWrapper<Role>()
                    .eq("role_name",roleName)
                    .ne("role_id",roleId));
            if( UtilTool.isNotBlank( list ) && list.size() > 0)
                return true;
        }
        //新增的时候根据名称查询
        if( UtilTool.isBlank( roleId ) && UtilTool.isNotBlank( roleName )){
            List<Role> list = roleMapper.selectList(new EntityWrapper<Role>()
                    .eq("role_name",roleName));
            if( UtilTool.isNotBlank( list ) && list.size() > 0)
                return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean updateRoleInfoById(Role roleInfo, Integer... menuIds) {
        if (roleInfo == null || roleInfo.getRoleId() == null) return false;
        Date nowTime = new Date();
        if (menuIds != null) {
            //构建条件删除
            Wrapper query = new EntityWrapper<Role>().eq("role_id",roleInfo.getRoleId());
            roleMenuMapper.delete(query);

            //新增角色菜单关系
            RoleMenu roleMenu ;
            for (int i = 0; i < menuIds.length; i++) {
                roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleInfo.getRoleId());
                roleMenu.setMenuId(menuIds[i]);
                roleMenu.setCreateTime(nowTime);

                roleMenuMapper.insert( roleMenu );
            }
        }
        roleInfo.setUpdateTime(nowTime);
        int rows = roleMapper.updateById(roleInfo);
        return rows > 0;
    }

    @Override
    @Transactional
    public boolean addNewSysRoleInfo(Role roleInfo, Integer... menuIds) {
        if (roleInfo == null ) return false;
        //新增角色
        roleInfo.setCreateTime( new Date());
        roleMapper.insert(roleInfo);
        //增加角色关联菜单
        if (menuIds != null && UtilTool.isNotBlank(roleInfo.getRoleId())) {
            RoleMenu roleMenu ;
            for (int i = 0; i < menuIds.length; i++) {
                roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleInfo.getRoleId());
                roleMenu.setMenuId(menuIds[i]);
                roleMenu.setCreateTime(new Date());

                roleMenuMapper.insert( roleMenu );
            }
        }
        return true;
    }

    @Override
    public List<Role> findAllRoleList() {

        Wrapper wrapper = new EntityWrapper<Role>();
        wrapper.ne("role_name","超级管理员");
        List<Role> roles = roleMapper.selectList(wrapper );
        return roles;
    }
}
